const { regClass } = Laya;
import GameManager from "./GameManager";
import { SceneLoadingBase } from "./SceneLoading.generated";

@regClass()
export class SceneLoading extends SceneLoadingBase {
    onEnable(): void {
        this.loadingBar.value = 0;
        this.loardToolInfoConfig();
        this.loadAudioConfig();
        this.getLocalStage();
    } 

    //加载工具配置文件
    loardToolInfoConfig() {
        const jsonPath: string = "./resources/config/config_toolInfo.json";
        Laya.loader.load(jsonPath, Laya.Loader.JSON).then((json) => {
            GameManager.getInstance().toolInfo = json.data;
            console.log("【GameManager.getInstance().toolInfo】", GameManager.getInstance().toolInfo);
        })

    }

    //加载音乐配置文件
    loadAudioConfig() {
        const jsonPath: string = "./resources/config/config_audio.json";
        Laya.loader.load(jsonPath, Laya.Loader.JSON).then((json) => {
            let audioPath = json.data;
            console.log("【audioPath】", audioPath);
            this.loadAudio(audioPath);
        })

    }

    loadAudio(path: string[]) {
        Laya.loader.load(path,null,Laya.Handler.create(this,this.onLoading,null,false))
            .then((result) => {
                console.log("【result】", result);
                if(result){
                    Laya.Scene.open("./resources/Scenes/Scene_Start.ls");
                }
            })
    }

    //加载时侦听
    onLoading(progress: number): void {
        this.loadingBar.value = progress;
        console.log("【progress】", progress);
    }

    getLocalStage(){
        // localStorage.removeItem("level"); 

        let level= localStorage.getItem("level");
        console.log("【level】", level);
        if(level===null){
            localStorage.setItem("level","1");
            GameManager.getInstance().level = 1;
        }
        else{
            GameManager.getInstance().level = parseInt(level);

        }
    }

}